package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.Block;
import com.grj.park.service.BlockService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("block")
public class BlockController {

    private final BlockService blockService;

    @GetMapping
    public R<List<Block>> getBlock(){
        return R.ok(blockService.list());
    }

    @PostMapping
    public R<?> saveBlock(@RequestBody Block block){
        LambdaQueryWrapper<Block> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Block::getGroupName, block.getGroupName());
        Block one = blockService.getOne(queryWrapper);
        if (one != null){
            return R.fail("组团名字已存在");
        }
        blockService.save(block);
        return R.okMsg("添加组团成功");
    }
}
